package com.ailot.cloud.upms.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ailot.cloud.upms.api.mapper.SysMenuMapper;
import com.ailot.cloud.upms.api.mapper.SysRoleMenuMapper;
import com.ailot.cloud.upms.api.service.SysMenuService;
import com.ailot.cloud.upms.common.entity.SysMenu;
import com.ailot.cloud.upms.common.entity.SysRoleMenu;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单权限
 */
@Service
@AllArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    private final SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> getMenusByRoleId(String roleId) {
        return baseMapper.getMenusByRoleId(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addMenu(SysMenu sysMenu) {
        String roleId = sysMenu.getRoleId();
        this.save(sysMenu);
        if (StrUtil.isNotBlank(roleId)){
            sysRoleMenuMapper.insert(new SysRoleMenu(roleId, sysMenu.getId()));
        }
    }
}
